package engine.operators.util;

import java.util.Collections;
import java.util.List;

import engine.ObjectiveFunction;
import engine.individuals.util.IndividualsComparator;

public class StatisticUtils<T> {

	private ObjectiveFunction<T> objFunction;
	
	private IndividualsComparator<T> individualsComparator;

	
	public StatisticUtils(ObjectiveFunction<T> objFunction) {
		this.objFunction = objFunction;
		this.individualsComparator = new IndividualsComparator<T>(objFunction);
	}

	public T min(List<T> individus) {
		return Collections.min(individus, individualsComparator);
	}
	
	public T max(List<T> individuals) {
		return Collections.max(individuals, individualsComparator);
	}
	
	public double stdDev(
			List<T> individuals,
			double mean) 
	{

		double stdDev = 0.0d;
		for (T individual : individuals) 
		{
			stdDev += Math.pow((objFunction.compute(individual) - mean),2);
		}
		stdDev = Math.sqrt(stdDev/(individuals.size()-1.0d));

		return stdDev;
	}

	public double mean(List<T> individuals) {

		double sum = 0.0d;

		for (T individual : individuals) {
			sum += objFunction.compute(individual);
		}

		sum /= individuals.size();
		
		return sum;
	}
}
